package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.PageModel;

import admin.data.PageData;
import admin.forms.PageForm;

public class PageServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		
		//Get config name
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.toLowerCase().replaceAll("/admin/page/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		String title = getTitle(id);
		
		if (title == null) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		PageData pageData = new PageData();
		PageModel page = pageData.getConfig(id);
		
		if (page == null) {
			page = new PageModel();
			page.setId(id);
		}
		
		request.setAttribute("page", page);
		request.setAttribute("check", check);
		request.setAttribute("title", title);
		try {
			request.getRequestDispatcher("/admin/pages/static_page.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		
		//Get config name
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.toLowerCase().replaceAll("/admin/page/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		String title = getTitle(id);
		
		if (title == null) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		PageData pageData = new PageData();
		PageModel page = pageData.getConfig(id);
		
		if (page == null) {
			page = new PageModel();
			page.setId(id);
		}
		
		PageForm pageForm = new PageForm();
		TreeMap form = pageForm.checkValid(request, page);
		check = (TreeMap) form.get("checkValid");
		page = (PageModel) form.get("page");
		
		if (check.size() == 0) {
			page.setUpdate(new Date());
			pageData.insert(page);
			request.setAttribute("success", "Trang " + title + " đã được thay đổi.");
		}
		
		request.setAttribute("page", page);
		request.setAttribute("check", check);
		request.setAttribute("title", title);
		try {
			request.getRequestDispatcher("/admin/pages/static_page.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	private String getTitle(String id) {
		String title = null;
		
		if (id.equalsIgnoreCase("about-us")) {
			title = "Về chúng tôi";
		}
		
		if (id.equalsIgnoreCase("regulation")) {
			title = "Quy chế sàn giao dịch";
		}
		
		if (id.equalsIgnoreCase("business-cooperation")) {
			title = "Cho doanh nghiệp.";
		}
		
		if (id.equalsIgnoreCase("recruitment")) {
			title = "Cơ hội nghề nghiệp";
		}
		
		if (id.equalsIgnoreCase("buying-guide")) {
			title = "Hỗ trợ mua hàng.";
		}
		
		if (id.equalsIgnoreCase("returns-policy")) {
			title = "Chính sách trả hàng";
		}
		
		if (id.equalsIgnoreCase("delivery")) {
			title = "Giao nhận hàng";
		}
		
		if (id.equalsIgnoreCase("payments")) {
			title = "Hình thức thanh toán";
		}
		
		if (id.equalsIgnoreCase("contact")) {
			title = "Liên hệ.";
		}
		
		return title;
	}
}
